<?php
//error_reporting(E_ALL);

include_once 'questions.php';
include_once 'layout.php';
include_once 'config.php';

ini_set("session.gc_maxlifetime", "10800");

session_start();

//==================================================
// print the header
//==================================================


function printHeader() {
	echo '<script type="text/javascript" src="sha1-min.js"></script>';
	?>
<SCRIPT type="text/javascript">
function submitform() {
	document.loginform.password.value = hex_sha1(document.loginform.password.value);
	document.loginform.submit();
}

function validateForm() {
	var checked = 0;
	var total = document.questions.elements.length;
	for(i = 0; i < total; i++)
	{
		if(document.questions.elements[i].type == "checkbox") {
			if(document.questions.elements[i].checked)
				checked++;
		}
	}
	
	// adjust this for more checkboxes
	if(checked > <?php echo Config::number_of_important_questions; ?>) {
		alert("ERROR: You have to select exactly <?php echo Config::number_of_important_questions; ?> important questions. \nPlease remove excess selections. ");
		return false;
	}

	// adjust this for more checkboxes
	if(checked < <?php echo Config::number_of_important_questions; ?>) {
		alert("ERROR: You have to select exactly <?php echo Config::number_of_important_questions; ?> important questions. \nPlease mark more questions as \'important\'.");
		return false;
	}
	
	return true;
}

function showHideDiv(id) {
	var elem = document.getElementById(id);
	
	if(elem.style.display == "none")
		elem.style.display = "inline";
	else
		elem.style.display = "none";
}
</SCRIPT>
	<?php
}

//==================================================
// print the form, either login or questions
//==================================================
function printForm() {
	if(IsSet($_SESSION['candidate']) && IsSet($_SESSION['election']))
	{
		echo 'Welcome ' . stripslashes($_SESSION['candidate']) . ' (<a href="login.php?logout=1">log out</a>)'; 
		echo '<br /><br />';
		echo 'Please answer these questions carefully. You must mark exactly ' . Config::number_of_important_questions . ' questions as \'important\'. The important questions are weighted heavier when we calculate the matching score with a voter. If you wish to explain your answer, just click the [Explain answer] link and fill in your explanation.<br /><br />';
		echo '<b>Notice:</b> If you take longer than 20 minutes to fill in the questionaire, please save your answers (hit submit) before the 20 minute mark. You will be able to continue answering where you left off after saving them.<br /><br /> ';
		printQuestions('candidate_edit.php', true);
	}
	else {
		if(!Config::matching_active) {
		?>
<form action="login.php" method="post" name="loginform">
	Candidate name: <input type="text" name="name"></input><br />
	Password: <input type="password" name="password"></input><br />
	<input onClick="javascript: submitform(); return false;" type="submit" /><br />
</form>
		<?php
		}
		else
			echo 'Matching has been enabled, and all candidate answers are locked down.';
	}
}

openHeader();
printHeader();
closeHeader();
openBody(5);
?>
<h1>Candidate management</h1>
<?php 
printForm();
closeHolder();
closeBody();  
?>
